
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;


public class frmCreateTroubleTicket extends JPanel
{
   JTextField txtDescription = new JTextField();
   JButton btnSave = new JButton();
   JButton btnCancel = new JButton();
   Properties displayCustomer;
   JPanel me = this;

   /**
    * Default constructor
    */
   public frmCreateTroubleTicket()
   {
      initializePanel();
   }
   
   public frmCreateTroubleTicket(Properties accRep, JFrame caller)
   {
	   initializePanel();
	   displayCustomer = accRep;
	   
	   caller.setVisible(false);
	   caller.dispose();
   }

   /**
    * Adds fill components to empty cells in the first row and first column of the grid.
    * This ensures that the grid spacing will be the same as shown in the designer.
    * @param cols an array of column indices in the first row where fill components should be added.
    * @param rows an array of row indices in the first column where fill components should be added.
    */
   void addFillComponents( Container panel, int[] cols, int[] rows )
   {
      Dimension filler = new Dimension(10,10);

      boolean filled_cell_11 = false;
      CellConstraints cc = new CellConstraints();
      if ( cols.length > 0 && rows.length > 0 )
      {
         if ( cols[0] == 1 && rows[0] == 1 )
         {
            /** add a rigid area  */
            panel.add( Box.createRigidArea( filler ), cc.xy(1,1) );
            filled_cell_11 = true;
         }
      }

      for( int index = 0; index < cols.length; index++ )
      {
         if ( cols[index] == 1 && filled_cell_11 )
         {
            continue;
         }
         panel.add( Box.createRigidArea( filler ), cc.xy(cols[index],1) );
      }

      for( int index = 0; index < rows.length; index++ )
      {
         if ( rows[index] == 1 && filled_cell_11 )
         {
            continue;
         }
         panel.add( Box.createRigidArea( filler ), cc.xy(1,rows[index]) );
      }

   }

   /**
    * Helper method to load an image file from the CLASSPATH
    * @param imageName the package and name of the file to load relative to the CLASSPATH
    * @return an ImageIcon instance with the specified image file
    * @throws IllegalArgumentException if the image resource cannot be loaded.
    */
   public ImageIcon loadImage( String imageName )
   {
      try
      {
         ClassLoader classloader = getClass().getClassLoader();
         java.net.URL url = classloader.getResource( imageName );
         if ( url != null )
         {
            ImageIcon icon = new ImageIcon( url );
            return icon;
         }
      }
      catch( Exception e )
      {
         e.printStackTrace();
      }
      throw new IllegalArgumentException( "Unable to load image: " + imageName );
   }

   public JPanel createPanel()
   {
      JPanel jpanel1 = new JPanel();
      FormLayout formlayout1 = new FormLayout("FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:88PX:NONE,FILL:DEFAULT:NONE,FILL:83PX:NONE,FILL:DEFAULT:NONE","CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE");
      CellConstraints cc = new CellConstraints();
      jpanel1.setLayout(formlayout1);

      JLabel jlabel1 = new JLabel();
      jlabel1.setFont(new Font("Tahoma",Font.BOLD,18));
      jlabel1.setText("Create Trouble Ticket");
      jpanel1.add(jlabel1,cc.xywh(2,2,5,1));

      JLabel jlabel2 = new JLabel();
      jlabel2.setText("Description:");
      jpanel1.add(jlabel2,cc.xy(2,4));

      txtDescription.setName("txtDescription");
      jpanel1.add(txtDescription,cc.xywh(4,4,3,1));

      btnSave.setActionCommand("Save");
      btnSave.setName("btnSave");
      btnSave.setText("Save");
      jpanel1.add(btnSave,cc.xy(4,6));

      btnCancel.setActionCommand("Cancel");
      btnCancel.setName("btnCancel");
      btnCancel.setText("Cancel");
      jpanel1.add(btnCancel,cc.xy(6,6));

      addFillComponents(jpanel1,new int[]{ 1,2,3,4,5,6,7 },new int[]{ 1,2,3,4,5,6,7 });
      
      btnSave.addActionListener( new ActionListener()
	    {
	    	public void actionPerformed( ActionEvent evt )
	    	{
	    		 
	    		ClientHelper.NewTroubleTicket(displayCustomer, txtDescription.getText(), me);
	    		ClientHelper.CustomerLogout(displayCustomer.getProperty(Constants.ACCOUNT_NUMBER));
	    		
	    		frmManageCustomer form = new frmManageCustomer(displayCustomer.getProperty(Constants.ACCOUNT_NUMBER),
						  displayCustomer.getProperty(Constants.PIN),
						  (JFrame)me.getParent().getParent().getParent().getParent());
	    		JScrollPane scrollable = new JScrollPane(form);
	    		JFrame newFrame = new JFrame();
	            newFrame.add(scrollable);
	            newFrame.pack();
	            newFrame.setSize(650, 677);
	            newFrame.setLocationRelativeTo(null);
	    		newFrame.setVisible(true);
	    	}
	    });
      
      btnCancel.addActionListener( new ActionListener()
	    {
	    	public void actionPerformed( ActionEvent evt )
	    	{
	    		  frmManageCustomer form = new frmManageCustomer(displayCustomer.getProperty(Constants.ACCOUNT_NUMBER), displayCustomer.getProperty(Constants.PIN), (JFrame)me.getParent().getParent().getParent().getParent());
		    		JScrollPane scrollable = new JScrollPane(form);
		    		JFrame newFrame = new JFrame();
		            newFrame.add(scrollable);
		            newFrame.pack();
		            newFrame.setSize(650, 677);
		            newFrame.setLocationRelativeTo(null);
		    		newFrame.setVisible(true);
	    	}
	    });
      return jpanel1;
   }

   /**
    * Initializer
    */
   protected void initializePanel()
   {
      setLayout(new BorderLayout());
      add(createPanel(), BorderLayout.CENTER);
   }


}
